package cn.the.angry.model;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import cn.the.angry.resourcemanager.ResourceManager;
import cn.the.angry.sprite.Sprite;

public abstract class Actor {
	protected World world;
	protected final Sprite sprite;
	private Sprite bombSprite;
//	protected AngryShape shape;
	protected boolean isBinding=false;
	protected int hp=100;
	protected int fullHp=hp;
	protected final boolean canDie;
	protected final int w;
	protected final int h;
	private int bombCount = 0;
	private int score;
	
	public Actor(final BufferedImage[] frames, final boolean canDie){
		sprite = new Sprite(frames);
		this.canDie=canDie;
		w = getSprite().getWidth();
		h = getSprite().getHeight();
	}
	
	public Actor(BufferedImage[] frames, final int hp)
	{
		this(frames, true);
		this.hp=hp;
		this.fullHp=hp;
	}
	
	public void setWorld(final World world)
	{
		this.world=world;
	}
	
	public abstract Body getBody();
	
	public abstract BodyDef getBodyDef();
	
/*	public Body getBody() {
		return shape.getBody();
	}

	public BodyDef getBodyDef() {
		return shape.getBodyDef();
	}*/
	
	public abstract void bind();
	
	
	//draw 
	public void draw(Graphics2D g2) {
		if(!isDead()){
			if(isBinding()){
				bind();
			}		
			//System.out.println((float)(hp/fullHp));
			getSprite().draw(g2);
		}else{
			if(bombSprite != null){
				if(bombCount < bombSprite.getFrameSize()+1){
					bombSprite.draw(g2);
				}
				bombCount++;
			}
		}
	}

	public Sprite getSprite() {
		return sprite;
	}

	public void setBinding(boolean isBinding) {
		this.isBinding = isBinding;
	}

	public boolean isBinding() {
		return isBinding;
	}
	
	public int getHP()
	{
		return hp;
	}
	
	public int damage(final int hp)
	{
		needChangedPic(hp);
		int oldHp, diffHp;
		if(!canDie || isDead()) return -1;
		else
		{
			oldHp=this.hp;
			this.hp-=Math.max(hp-20,0);
			diffHp=oldHp-this.hp;
		}
		if(isDead())
		{
			new Thread()
			{
				@Override
				public void run() 
				{
					while(true)
					{
						try
						{
							Thread.sleep(100);
						}
						catch (InterruptedException e)
						{
							e.printStackTrace();
						}
						final int count=world.getBodyCount();
						world.destroyBody(getBody());
						
						if(count!=world.getBodyCount()){
							setBinding(false);
							setBombSprite(new Sprite(ResourceManager.getBombImage()));
							bombSprite.setAttributes(sprite.getX(), sprite.getY(), sprite.getAngle());
							bombSprite.setActive(true);
							bombSprite.setUpdateTime(80);
							break;
						}
							
					}
				};
			}.start();
		}
		return diffHp;
	}
	
	//revision 105
	public void startBomb(){
		new Thread()
		{
			@Override
			public void run() 
			{
				while(true)
				{
					try
					{
						Thread.sleep(100);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					final int count=world.getBodyCount();
					world.destroyBody(getBody());
					
					if(count!=world.getBodyCount()){
						setBinding(false);
						setBombSprite(new Sprite(ResourceManager.getBombImage()));
						bombSprite.setAttributes(sprite.getX(), sprite.getY(), sprite.getAngle());
						bombSprite.setActive(true);
						bombSprite.setUpdateTime(80);
						break;
					}
						
				}
			};
		}.start();
	}
	
	public boolean canDie()
	{
		return canDie;
	}
	
	public boolean isDead()
	{
		return hp<=0;
	}
	
	public void kill()
	{
		hp=0;
	}
	
	public void setHP(int _hp){
		hp = _hp;
	}
	
	protected void needChangedPic(int damagehp){
		
	}

	protected void setBombSprite(Sprite bombSprite) {
		this.bombSprite = bombSprite;
	}

	protected Sprite getBombSprite() {
		return bombSprite;
	}

	public int getScore()
	{
		return score;
	}

	public void setScore(int score)
	{
		this.score = score;
	}
	
	public int getFullHp()
	{
		return fullHp;
	}
}
